import api from '../../../api/index';
import { iconData } from './iconData'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    dataList: [],
    dataListdesc: [],
    allRecordItemList: [],
    shape: null,
    birday: null,
    gender: 1, // 性别 1男 2女
    staticImage:{
        iconOpen: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/xia.png',
        iconFold: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/weightManagement/shang.png'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { id } = options
    this.getLoadDesc(id);
    this.setData({
      gender: wx.getStorageSync('userInfo').gender,
      birday: wx.getStorageSync('userInfo').birthday,
    })
  },
  findIndex(arr, target) {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] === target) {
        return i;
      }
    }
    return -1;
  },
  calculateAge(birthDate) {
    const birth = new Date(birthDate);
    const today = new Date();
  
    let age = today.getFullYear() - birth.getFullYear();
    const monthDifference = today.getMonth() - birth.getMonth();
  
    // 如果还没有到生日，年龄减一
    if (monthDifference < 0 || (monthDifference === 0 && today.getDate() < birth.getDate())) {
      age--;
    }
  
    return age;
  },
  formatWeightData(data) {
    let height = Number(this.data.allRecordItemList.find(item => item.code === 'XYZBXY001013').value)
    let sw = 0 // 标准体重
    if(this.data.gender == 1) { // 男性
      sw = (height - 60) * 0.7
    } else {
      sw = ((height * 1.37) - 110) * 0.45
    }
    let ticks = [(0.8 * sw).toFixed(2), (0.9 * sw).toFixed(2), (1.1 * sw).toFixed(2), (1.2 * sw).toFixed(2)]
    let level = ''
    let advice = ''
    if (Number(data.value) < Number(ticks[0])) {
      level = '严重偏低'
      advice = '长期体重过轻会导致一系统列问题，如脱发、厌食症等，身体机能会下降，需要加强营养，多吃高蛋白食物，摄入更多的热量以增加体重。'
    } else if (Number(data.value) >= Number(ticks[0]) && Number(data.value) <= Number(ticks[1])) {
      level = '偏低'
      advice = '体重偏低，身体消瘦，建议加强营养，平衡饮食，多吃高蛋白食物，摄入更多的热量以增加体重。'
    } else if (Number(data.value) > Number(ticks[1]) && Number(data.value) <= Number(ticks[2])) {
      level = '标准'
      advice = '恭喜您拥有理想的体重，保持合理健康的生活方式，适量参加运动，您就可以维持标准体重了。'
    } else if (Number(data.value) > Number(ticks[2]) && Number(data.value) <= Number(ticks[3])) {
      level = '偏高'
      advice = '体重偏重，略显肥胖，建议一周进行３-５次有氧运动，减少主食（米饭面食等）的摄入，增加高纤维粗粮比例。'
    } else {
      level = '严重偏高'
      advice = '体重严重超标，建议低脂、低胆固醇、高纤维膳食，补充多种维生素，增加运动量进行体重控制。'
    }
    return {
      level: level,
      advice: advice,
      ticks: ticks,
      ids: this.findIndex(data.levels, level)
    }
  },
  formatBMIData(data) {
    let ticks = [18.5, 25]
    let level = ''
    let advice = ''
    let height = Number(this.data.allRecordItemList.find(item => item.code === 'XYZBXY001013').value)
    if (Number(data.value) < Number(ticks[0])) {
      level = '偏低'
      advice = '需要提升体能健康增重，适当多吃高热量、高蛋白、高脂肪饮食，多做力量运动如举重、俯卧撑、仰卧起坐等'
    } else if (Number(data.value) >= Number(ticks[0]) && Number(data.value) <= Number(ticks[1])) {
      level = '标准'
      let value = 0
      if(this.data.gender == 1) { // 男性
        value = (height * 1/2) -10
      } else {
        value = (height * 1/2) -13
      } 
      advice = `BMI达标，如果腰围也属于建议的尺寸${value.toFixed(0)}cm就更加理想了。`
    } else {
      level = '偏高'
      advice = 'BMI超标，建议选择比较健康的方法减重，如控制饮食、改变不良生活习惯和参加跑步、跳绳、打篮球、踢足球等消耗体能的运动'
    } 
    return {
      level: level,
      advice: advice,
      ticks: ticks,
      ids: this.findIndex(data.levels, level)
    }
  },
  formatBodyfatData(data) {
    let ticks = []
    let level = ''
    let advice = ''
    if(this.data.gender == 1) { // 男性
      ticks = [11, 21, 26]
    } else {
      ticks = [21, 31, 36]
    } 
    if (Number(data.value) < Number(ticks[0])) {
      level = '偏低'
      advice = '当身体摄取到优质营养，并且令到小肠绒毛正常运作，就可以达到正常的脂肪比例。为了增重，食物最好以易消化、高蛋白、高热量为原则。'
    } else if (Number(data.value) >= Number(ticks[0]) && Number(data.value) <= Number(ticks[1])) {
      level = '标准'
      advice = '目前您的体脂率处于标准范围，保持好的饮食方式和生活习惯是保持健康身材的最佳途径。'
    } else if (Number(data.value) > Number(ticks[1]) && Number(data.value) <= Number(ticks[2])) {
      level = '偏高'
      advice = '要匀称不显胖，每日有氧运动要持续30分钟，体脂率才会开始燃烧，快走、慢跑、游泳、爬楼梯、骑自行车都是很好的选择'
    }  else {
      level = '严重偏高'
      advice = '您的体内囤积了太多脂肪，必须检测血压、血糖、肝功能等情况，是否潜藏危害。赶快开始您的减肥大战，坚持饮食控制、运动及改变生活方式。'
    } 
    return {
      level: level,
      advice: advice,
      ticks: ticks,
      ids: this.findIndex(data.levels, level)
    }
  },
  formatWaterData(data) {
    let ticks = []
    let level = ''
    let advice = ''
    if(this.data.gender == 1) { // 男性
      ticks = [55, 65]
    } else {
      ticks = [45, 60]
    } 
    if (Number(data.value) < Number(ticks[0])) {
      level = '偏低'
      advice = '体水分率偏低，规律的饮食习惯和每天喝足8杯水可以维持正常的体水分水平，充足的水分可以促进代谢，带走废物和身体毒素。'
    } else if (Number(data.value) >= Number(ticks[0]) && Number(data.value) <= Number(ticks[1])) {
      level = '标准'
      advice = '身体水分率处于标准值，适量饮水，适当运动，均衡饮食，保持身体水分的平衡。'
    } else{
      level = '充足'
      advice = '身体水分含量高，细胞活性高。充足的水分能帮助您更好地消化食物和吸收养分，并促进代谢，带走废物和毒素。'
    } 
    return {
      level: level,
      advice: advice,
      ticks: ticks,
      ids: this.findIndex(data.levels, level)
    }
  },
  formatMuscleMassData(data) {
    let ticks = []
    let level = ''
    let advice = ''
    if(this.data.gender == 1) { // 男性
      ticks = [49, 59]
    } else {
      ticks = [40, 50]
    } 
    if (Number(data.value) < Number(ticks[0])) {
      level = '偏低'
      advice = '您的骨胳肌比率低于理想范围，跟多静态活动、不运动有关，会导致基础代谢率降低，腰酸背痛，力量下降，外在表现是发胖，也容易诱发心血管疾病。'
    } else if (Number(data.value) >= Number(ticks[0]) && Number(data.value) <= Number(ticks[1])) {
      level = '标准'
      advice = '您的骨胳肌比率处于标准范围。运动量过少或者节食会导致肌肉流失，请保持适当的运动量和合理的饮食。'
    } else{
      level = '偏高'
      advice = '如果脂肪比例正常，您是一个比较喜欢运动的人，适当的骨胳肌比率能够显示您健壮的体型，但过高的骨胳肌比率可能会影响您的灵活性。如果脂肪比例偏低，您的身材可能偏瘦，平衡身体各项参数，您就能拥有健康标准的身材。'
    } 
    return {
      level: level,
      advice: advice,
      ticks: ticks,
      ids: this.findIndex(data.levels, level)
    }
  },
  formatMuscleData(data) {
    let height = Number(this.data.allRecordItemList.find(item => item.code === 'XYZBXY001013').value)
    let ticks = []
    let level = ''
    let advice = ''
    if(this.data.gender == 1) { // 男性
      if(height < 160) {
        ticks = [38.5,46.5]
      } else if (height >= 160 && height <= 170) {
        ticks = [44,52.4]
      } else {
        ticks = [49.4,59.4]
      }
    } else {
      if(height < 150) {
        ticks = [29.1,34.7]
      } else if (height >= 150 && height <= 160) {
        ticks = [32.9,37.5]
      } else {
        ticks = [36.5,42.5]
      }
    } 
    if (Number(data.value) < Number(ticks[0])) {
      level = '偏低'
      advice = '您缺少足够的肌肉，需要加强运动，增加肌肉。'
    } else if (Number(data.value) >= Number(ticks[0]) && Number(data.value) <= Number(ticks[1])) {
      level = '标准'
      advice = '您的肌肉比较发达，继续保持。'
    } else{
      level = '充足'
      advice = '您的肌肉比较发达，继续保持。'
    } 
    return {
      level: level,
      advice: advice,
      ticks: ticks,
      ids: this.findIndex(data.levels, level)
    }
  },
  formatBoneData(data) {
    let weight = Number(this.data.allRecordItemList.find(item => item.code === 'XYZBXY001007').value)
    let ticks = []
    let level = ''
    let advice = ''
    if(this.data.gender == 1) { // 男性
      if(weight <= 60) {
        ticks = [2.3, 2.7]
      } else if (weight > 60 && weight < 75) {
        ticks = [2.7, 3.1]
      } else {
        ticks = [3.0, 3.4]
      }
    } else {
      if(weight <= 45) {
        ticks = [1.6, 2.0]
      } else if (weight > 45 && weight < 60) {
        ticks = [2.0, 2.4]
      } else {
        ticks = [2.3,2.7]
      }
    } 
    if (Number(data.value) < Number(ticks[0])) {
      level = '偏低'
      advice = '您的骨量水平偏低。长期低钙饮食、缺乏运动、过度减肥等都可能引起骨量偏低，多吃含钙高的食物，多晒太阳，多运动及时补钙。'
    } else if (Number(data.value) >= Number(ticks[0]) && Number(data.value) <= Number(ticks[1])) {
      level = '标准'
      advice = '您的骨量水平标准。骨量在短期内不会出现明显的变化，您只要保证健康的饮食和适当的锻炼，就可以维持稳定健康的骨量水平。'
    } else{
      level = '偏高'
      advice = '您的骨量水平偏高。说明骨骼中包含的钙等无机盐的含量非常充分，但要注意防范肾结石、低血压的风险，尽量避免高钙摄入。'
    } 
    return {
      level: level,
      advice: advice,
      ticks: ticks,
      ids: this.findIndex(data.levels, level)
    }
  },
  formatProteinData(data) {
    let ticks = []
    let level = ''
    let advice = ''
    if(this.data.gender == 1) { // 男性
      ticks = [16, 18]
    } else {
      ticks = [14, 16]
    } 
    if (Number(data.value) < Number(ticks[0])) {
      level = '偏低'
      advice = '蛋白质不足会引起基础代谢减少，也会引起肌肉的数量减少。坚持长期运动，适当提高肌肉比例，辅助于蛋白质的补充，可以提升蛋白质比例。'
    } else if (Number(data.value) >= Number(ticks[0]) && Number(data.value) <= Number(ticks[1])) {
      level = '标准'
      advice = '您的蛋白质处于标准水平。'
    } else{
      level = '充足'
      advice = '蛋白质比例充足。'
    } 
    return {
      level: level,
      advice: advice,
      ticks: ticks,
      ids: this.findIndex(data.levels, level)
    }
  },
  formatSubfatData(data) {
    let ticks = []
    let level = ''
    let advice = ''
    if(this.data.gender == 1) { // 男性
      ticks = [8.6, 16.7]
    } else {
      ticks = [18.5, 26.7]
    } 
    if (Number(data.value) < Number(ticks[0])) {
      level = '偏低'
      advice = '适量的皮下脂肪能够保护内脏和抵御寒冷，适量增加高蛋白、高热量食物可以增加脂肪。'
    } else if (Number(data.value) >= Number(ticks[0]) && Number(data.value) <= Number(ticks[1])) {
      level = '标准'
      advice = '您的皮下脂肪率处于标准范围。适当的运动量和合理的饮食就能保持适量的皮下脂肪。'
    } else{
      level = '偏高'
      advice = '皮下脂肪过多是外表肥胖的主要原因，除了有氧减脂以外，多进行增肌训练，肌肉的增加可以让您拥有更完美的体型。'
    } 
    return {
      level: level,
      advice: advice,
      ticks: ticks,
      ids: this.findIndex(data.levels, level)
    }
  },
  formatVisfatData(data) {
    let ticks = [9,14]
    let level = ''
    let advice = ''
    if (Number(data.value) < Number(ticks[0])) {
      level = '标准'
      advice = '内脏脂肪指数标准，暂时没有太大风险。'
    } else if (Number(data.value) === Number(ticks[0])) {
      level = '标准'
      advice = '虽然处于标准范围，但内脏脂肪已经开始堆积，请积极运动，改变久坐不动、饮食不均衡等不良习惯。'
    } else if (Number(data.value) > Number(ticks[0]) && Number(data.value) <= Number(ticks[1])) {
      level = '偏高'
      advice = '内脏脂肪指数偏高，持续保持均衡的饮食和适当的运动，以标准程度为目标，进行适当运动和限制卡路里。'
    } else{
      level = '严重偏高'
      advice = '内脏脂肪指数危险，罹患心脏病、高血压、高血脂和Ⅱ型糖尿病风险大，您迫切需要控制体重、积极运动和限制饮食。'
    } 
    return {
      level: level,
      advice: advice,
      ticks: ticks,
      ids: this.findIndex(data.levels, level)
    }
  },
  formatBodyshapeData(data) {
    let description = ''
    if (data.value === '隐形肥胖型') {
      description = '您的体型属于隐形肥胖型，得多进行有氧运动，否则很容易成为真胖子了。'
    } else if (data.value === '偏胖型') {
      description = '您的体型属于偏胖型，控制饮食和加强有氧运动能够助您降低脂肪。'
    } else if (data.value === '肥胖型') {
      description = '您的体型属于肥胖型，控制饮食和加强有氧运动能够助您降低脂肪。'
    } else if (data.value === '偏瘦肌肉型') {
      description = '您的体型属于偏瘦肌肉型，继续保持。'
    } else if (data.value === '标准型') {
      description = '您的体型属于标准型，继续保持。'
    } else if (data.value === '非常肌肉型') {
      description = '您的体型属于非常肌肉型，继续保持。'
    } else if (data.value === '偏瘦型') {
      description = '您的体型属于偏瘦型，需要加强营养了。'
    } else if (data.value === '运动不足型') {
      description = '您的体型属于运动不足型，需要运动起来了。'
    } else if (data.value === '标准肌肉型') {
      description = '您的体型属于标准肌肉型，继续保持。'
    }
    return {
      description: description,
    }
  },
  formatBmrData(data) {
    let age = this.calculateAge(this.data.birday)
    let sm = 0
    let level = ''
    let description = ''
    if(this.data.gender == 1) { // 男性
      if (age >=18 && age <=29) {
        sm = 24
      } else if (age >=30 && age <=49) { 
        sm = 22.3
      } else if (age >=50 && age <=69) { 
        sm = 21.5
      } else if (age >70) { 
        sm = 21.5
      }
    } else {
      if (age >=18 && age <=29) {
        sm = 23.6
      } else if (age >=30 && age <=49) { 
        sm = 21.7
      } else if (age >=50 && age <=69) { 
        sm = 20.7
      } else if (age >70) { 
        sm = 20.7
      }
    } 
    if (Number(data.value) < sm) {
      level = '不达标'
      description =  `您的标准基础代谢率为${data.value}kcal,目前处于未达标状态。持续轻量运动能够提高身体的基础代谢率，而节食基础代谢会大幅下降。`
    } else {
      level = '达标'
      description =  `您的标准基础代谢率为${data.value}kcal,目前处于达标状态。保持基础代谢率最有效的方式是每天都进行适量的运动`
    }
    return {
      level: level,
      description: description,
      ids: level === '达标' ? 0 : 1
    }
  },
  getLoadDesc(examid) {
    api
      .post(`/api/exams/record/${examid}`, {
        data: {
        }
      })
      .then((res) => {
        if (res[0].devices[0].items.length > 0) {
          const reponseDataList = res[0].devices[0].items
          this.setData({
            allRecordItemList: reponseDataList
          })
          iconData.forEach(icon => {
            const findRes = reponseDataList.find(item => item.code === icon.code)
            if (findRes) {
              icon.iconFold = false
              icon.value = findRes.value
              if(icon.toFixedNum !== undefined && icon.toFixedNum !== null) {
                icon.value = parseFloat(icon.value).toFixed(icon.toFixedNum)
              }
              if(icon.code === 'XYZBXY001007') { // 体重
                icon.level = this.formatWeightData(icon).level
                icon.advice = this.formatWeightData(icon).advice
                icon.ticks = this.formatWeightData(icon).ticks
                icon.ids = this.formatWeightData(icon).ids
              } else if (icon.code === 'XYZBXY001001') {// BMI
                icon.level = this.formatBMIData(icon).level
                icon.advice = this.formatBMIData(icon).advice
                icon.ticks = this.formatBMIData(icon).ticks
                icon.ids = this.formatBMIData(icon).ids
              } else if (icon.code === 'lk1590141731679') {// 体脂率
                icon.level = this.formatBodyfatData(icon).level
                icon.advice = this.formatBodyfatData(icon).advice
                icon.ticks = this.formatBodyfatData(icon).ticks
                icon.ids = this.formatBodyfatData(icon).ids
              } else if (icon.code === 'XYZBXY001029') {// 体水分
                icon.level = this.formatWaterData(icon).level
                icon.advice = this.formatWaterData(icon).advice
                icon.ticks = this.formatWaterData(icon).ticks
                icon.ids = this.formatWaterData(icon).ids
              } else if (icon.code === 'HXJK00000002') {// 骨骼肌率
                icon.level = this.formatMuscleMassData(icon).level
                icon.advice = this.formatMuscleMassData(icon).advice
                icon.ticks = this.formatMuscleMassData(icon).ticks
                icon.ids = this.formatMuscleMassData(icon).ids
              } else if (icon.code === 'HXJK00000009') {// 肌肉量
                icon.level = this.formatMuscleData(icon).level
                icon.advice = this.formatMuscleData(icon).advice
                icon.ticks = this.formatMuscleData(icon).ticks
                icon.ids = this.formatMuscleData(icon).ids
              } else if (icon.code === 'XYZBXY001033') {// 骨量
                icon.level = this.formatBoneData(icon).level
                icon.advice = this.formatBoneData(icon).advice
                icon.ticks = this.formatBoneData(icon).ticks
                icon.ids = this.formatBoneData(icon).ids
              } else if (icon.code === 'lk1636612991835') {// 蛋白质
                icon.level = this.formatProteinData(icon).level
                icon.advice = this.formatProteinData(icon).advice
                icon.ticks = this.formatProteinData(icon).ticks
                icon.ids = this.formatProteinData(icon).ids
              } else if (icon.code === 'HXJK00000001') {// 皮下脂肪
                icon.level = this.formatSubfatData(icon).level
                icon.advice = this.formatSubfatData(icon).advice
                icon.ticks = this.formatSubfatData(icon).ticks
                icon.ids = this.formatSubfatData(icon).ids
              } else if (icon.code === 'XYZBXY001035') {// 内脏脂肪
                icon.level = this.formatVisfatData(icon).level
                icon.advice = this.formatVisfatData(icon).advice
                icon.ticks = this.formatVisfatData(icon).ticks
                icon.ids = this.formatVisfatData(icon).ids
              } else if (icon.code === 'HXJK00000014') {// 体型
                icon.description = this.formatBodyshapeData(icon).description
              } else if (icon.code === 'lk1590141731918') {// 基础代谢
                icon.level = this.formatBmrData(icon).level
                icon.description = this.formatBmrData(icon).description
                icon.ids = this.formatBmrData(icon).ids
              }
            }
          })
          let showList = []
          reponseDataList.forEach(item => {
            const findRes = iconData.find(icon => item.code === icon.code)
            if (findRes) {
              showList.push(findRes)
            }
          })
          this.setData({
            dataListdesc: showList
          })
        }
      });
  },
  toggleIconFold(e) {
    let index = e.currentTarget.dataset.index
    let value = `dataListdesc[${index}].iconFold`
    this.setData({
      [value]: !this.data.dataListdesc[index].iconFold
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})